package com.byb.server.service.impl;

import com.byb.server.service.StrichenService;
import com.byb.server.domain.Strichen;
import com.byb.server.repository.StrichenRepository;
import com.byb.server.repository.search.StrichenSearchRepository;
import com.byb.server.service.dto.StrichenDTO;
import com.byb.server.service.mapper.StrichenMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Strichen.
 */
@Service
@Transactional
public class StrichenServiceImpl implements StrichenService{

    private final Logger log = LoggerFactory.getLogger(StrichenServiceImpl.class);

    private final StrichenRepository strichenRepository;

    private final StrichenMapper strichenMapper;

    private final StrichenSearchRepository strichenSearchRepository;
    public StrichenServiceImpl(StrichenRepository strichenRepository, StrichenMapper strichenMapper, StrichenSearchRepository strichenSearchRepository) {
        this.strichenRepository = strichenRepository;
        this.strichenMapper = strichenMapper;
        this.strichenSearchRepository = strichenSearchRepository;
    }

    /**
     * Save a strichen.
     *
     * @param strichenDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public StrichenDTO save(StrichenDTO strichenDTO) {
        log.debug("Request to save Strichen : {}", strichenDTO);
        Strichen strichen = strichenMapper.toEntity(strichenDTO);
        strichen = strichenRepository.save(strichen);
        StrichenDTO result = strichenMapper.toDto(strichen);
        //strichenSearchRepository.save(strichen);
        return result;
    }

    /**
     *  Get all the strichens.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<StrichenDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Strichens");
        return strichenRepository.findAll(pageable)
            .map(strichenMapper::toDto);
    }

    /**
     *  Get one strichen by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public StrichenDTO findOne(Long id) {
        log.debug("Request to get Strichen : {}", id);
        Strichen strichen = strichenRepository.findOne(id);
        return strichenMapper.toDto(strichen);
    }

    /**
     *  Delete the  strichen by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Strichen : {}", id);
        strichenRepository.delete(id);
       // strichenSearchRepository.delete(id);
    }

    /**
     * Search for the strichen corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<StrichenDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Strichens for query {}", query);
        Page<Strichen> result = strichenSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(strichenMapper::toDto);
    }
}
